package cn.lanqiao.service.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.lanqiao.entity.Admin;
import cn.lanqiao.entity.Login;
import cn.lanqiao.entity.Student;
import cn.lanqiao.entity.Teacher;
import cn.lanqiao.mapper.LoginMapper;
import cn.lanqiao.mapper.StudentMapper;
import cn.lanqiao.mapper.TeacherMapper;

@Service
public class LoginServiceImpl implements LoginMapper {
	 @Autowired
	 private LoginMapper loginMapper;
	 @Autowired
     private TeacherMapper teacherMapper;
	 @Autowired
	 private StudentMapper studentMapper;

	@Override
	public Admin adminLogin(String username, String password) {
		 return loginMapper.adminLogin(username,password);
	}

	@Override
	public Teacher teacherLogin(String username, String password) {
		  return loginMapper.teacherLogin(username,password);
	}

	@Override
	public Student studentLogin(String username, String password) {
		return loginMapper.studentLogin(username,password);
	}

	@Override
	public Integer getUserByUsername(String username) {
		return loginMapper.getUserByUsername(username);
	}

	public String register(Login login, String code, HttpServletRequest request) {
		Integer userCount = loginMapper.getUserByUsername(login.getUsername());
		Student student = new Student();
		if(userCount == 0){
			student.setEmail(login.getUsername());
			student.setPwd(login.getPassword());
			studentMapper.add(student);
			return "学生注册成功!";
		}else{
			return "该邮箱已被注册!";
		}
	}
  public String teacherRegister(Login login, String code, HttpServletRequest request) {
	  Integer userCount = loginMapper.getUserByTeachername(login.getUsername());
		Teacher teacher = new Teacher();
		if(userCount == 0){
			teacher.setEmail(login.getUsername());
			teacher.setPwd(login.getPassword());
			teacherMapper.add(teacher);
			return "教师注册成功!";
		}else{
			return "该邮箱已被注册!";
		}
  }
	

	@Override
	public Integer getUserByTeachername(String username) {
		return loginMapper.getUserByTeachername(username);
	}
}
